package com.sockect3d;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MyTextView extends TextView {

    public MyTextView(Context context) {
        super(context);
    }

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    int mLastx = -1;

    int mLasty = -1;


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getRawX();
        int y = (int) event.getRawY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                if (mLasty != -1) {
                    RelativeLayout.LayoutParams linearParams = (RelativeLayout.LayoutParams)this.getLayoutParams();
                    linearParams.leftMargin +=(x-mLastx);
                    linearParams.topMargin +=(y-mLasty);
                    if(linearParams.leftMargin<0){
                        linearParams.leftMargin = 0;
                    }else if(linearParams.leftMargin>=(100*DensityUtil.getDisplayDensity())){
                        linearParams.leftMargin = (int)(100*DensityUtil.getDisplayDensity());
                    }
                    if(linearParams.topMargin<0){
                        linearParams.topMargin = 0;
                    }else if(linearParams.topMargin>=(100*DensityUtil.getDisplayDensity())){
                        linearParams.topMargin = (int)(100*DensityUtil.getDisplayDensity());
                    }
                    this.setLayoutParams(linearParams);
                }
                break;
            case MotionEvent.ACTION_UP:
                RelativeLayout.LayoutParams linearParams = (RelativeLayout.LayoutParams)this.getLayoutParams();
                linearParams.leftMargin = (int)(50*DensityUtil.getDisplayDensity());
                linearParams.topMargin = (int)(50*DensityUtil.getDisplayDensity());
                this.setLayoutParams(linearParams);
                break;
            default:
                break;
        }
        mLastx = x;
        mLasty = y;
        return true;
    }
}
